package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button btn1;
private ImageView img1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btn1Event();
    }

    private void btn1Event() {
       btn1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               //RotateAnimationEvent();
               //startAnimationSetWithJava();
               startAnimationSetWithXML();
           }
       });

    }

    private void startAnimationSetWithXML() {

           AnimationSet animationSet= (AnimationSet) AnimationUtils.loadAnimation
                   (MainActivity.this,
                           R.anim.set
           );
           img1.startAnimation(animationSet);
    }

    private void startAnimationSetWithJava() {
        AnimationSet animationSet=new AnimationSet(true);
        TranslateAnimation translateAnimation=new TranslateAnimation(0,300,0,300);
        ScaleAnimation scaleAnimation=new ScaleAnimation(1,3,1,3);
  RotateAnimation rotateAnimation=
               new RotateAnimation(0,360,180,180);
       animationSet.addAnimation(translateAnimation);
       animationSet.addAnimation(scaleAnimation);
       //animationSet.addAnimation(rotateAnimation);
        animationSet.setDuration(3000);
        animationSet.setRepeatCount(2);
        animationSet.setRepeatMode(Animation.REVERSE);

       img1.startAnimation(animationSet);
    }
    private void RotateAnimationEvent() {
      /* RotateAnimation rotateAnimation=
               new RotateAnimation(0,360,180,180);
       rotateAnimation.setDuration(3000);
       rotateAnimation.setRepeatCount(2);
       rotateAnimation.setRepeatMode(Animation.REVERSE);
       img1.startAnimation(rotateAnimation);*/

       RotateAnimation rotateAnimation1= (RotateAnimation) AnimationUtils.loadAnimation
               (MainActivity.this,R.anim.rotate);
       img1.startAnimation(rotateAnimation1);
    }
    private void initView() {
           btn1=findViewById(R.id.btn1);
           img1=findViewById(R.id.img1);
    }
}
